package com.applet.mallappletbackstage.controller;


import com.applet.mallappletbackstage.entity.WorkType;
import com.applet.mallappletbackstage.entity.vo.WorkTypeVO;
import com.applet.mallappletbackstage.service.WorkTypeService;
import com.applet.mallappletbackstage.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ${author}
 * @since 2020-07-31
 */
@RestController
@RequestMapping("/work-type")
public class WorkTypeController {

    @Autowired
    WorkTypeService workTypeService;

    @GetMapping("/all")
    public R getWorkType() {
        List<WorkType> list = workTypeService.list();
        ArrayList<String> workTypes = new ArrayList<>();
        for (WorkType workType : list) {
            String type = workType.getType();
            workTypes.add(type);
        }
        return R.success().data(workTypes);
    }

    /***
     * 保存工作种类
     * @param workTypeVO
     * @return
     */
    @PostMapping("/saveWorkType")
    public R saveWorkType(@RequestBody WorkTypeVO workTypeVO) {
        int count = workTypeService.saveWorkType(workTypeVO);
        if (count > 0) {
            return R.success("保存成功");
        }
        return R.fail("保存失败");
    }

    /***
     * 通过id获取工作类型
     * @return
     */
    @GetMapping("/getWorkTypeOne/{id}")
    public R getWorkTypeOne(@PathVariable(value = "id") Integer id) {
       WorkType workType = workTypeService.getWorkTypeOne(id);
        return R.success(workType);
    }


    @DeleteMapping("delete/{id}")
    public R deleteWorkType(@PathVariable("id") String id) {
        workTypeService.removeById(id);
        return R.success();
    }
}
